;;; 47ruby.el --- Custom ruby-mode configuration

;; Ruby mode
(require 'ruby-mode)
(require 'ruby-electric)
(require 'inf-ruby)
;;; (require 'rubydb)
;;; (require 'ri-emacs)

(setq ri-ruby-script "/home/josh/.emacs.d/libs/ri-emacs.rb")
(autoload 'ri "/home/josh/.emacs.d/libs/ri-ruby.el" nil t)

;;(add-hook 'ruby-mode-hook (lambda ()
;;                            ;;; (local-set-key 'f1 'ri)
;;                            (local-set-key "\M-\C-i" 'ri-ruby-complete-symbol)
;;                            (local-set-key 'f4 'ri-ruby-show-args)
;;                            ))

;; Set those modes!
(add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\(?:^\\|/\\)Rakefile$" . ruby-mode))
(add-to-list 'auto-mode-alist '("\\.gemspec$" . ruby-mode))
(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))

;; Like c-in-literal, only for Ruby
(defun ruby-in-literal ()
  (let* ((here (point))
         (state (save-excursion
                  (ruby-beginning-of-defun)
                  (parse-partial-sexp (point) here))))
    (or (nth 3 state)
        (nth 4 state)
        nil)))

;; MMM class for working with erb
(mmm-add-classes
 '((erb-code
    :submode ruby-mode
    :match-face (("<%#" . mmm-comment-submode-face)
                 ("<%=" . mmm-output-submode-face)
                 ("<%"  . mmm-code-submode-face))
    :front "<%[#=]?" 
    :back "-?%>" 
    :insert ((?% erb-code       nil @ "<%"  @ " " _ " " @ "%>" @)
             (?# erb-comment    nil @ "<%#" @ " " _ " " @ "%>" @)
             (?= erb-expression nil @ "<%=" @ " " _ " " @ "%>" @)))))

;; Some ruby-specific key-bindings
(add-hook 'ruby-mode-hook 'llasram/ruby-extra-keys)
(defun llasram/ruby-extra-keys ()
  (define-key ruby-mode-map "\C-m"      'newline-and-indent)
  (define-key ruby-mode-map "\C-c\C-a"  'ruby-annotate-buffer))

;; RI everywhere!
;;; (define-key help-map "\C-r" 'ri)
